const app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isman1:true,
    isman2:false,
    isman3:false,
    read:true,
    windowHeight:'',
    lunboArray:[],
    position:'成都',
    informationarray:[],
    companysArray:[],
    teachersArray:[],
    studentArray:[],
    lessonArray:[],
    videoArray:[]
  },
  toNewsTable:function(event){
    wx.navigateTo({
      url: '/eduNews/educationTable/educationTable',
    })
  },
  toCompanyTable:function(event){
    wx.navigateTo({
      url: '../findcompany/findcompany',
    })
  },
  toFindteacherTable:function(event){
      wx.navigateTo({
        url: '/shouye/findteacher/findteacher',
      })
  },
  toFindstudentTable:function(event){
    wx.navigateTo({
      url: '/shouye/findStudent/findStudent',
    })
  },
  toCourseCenter:function(event){
    wx.navigateTo({
      url: '/lesson/table/table',
    })
  },
  toVideoTable:function(event){
    wx.navigateTo({
      url: '/eduVedio/videoPage/videoPage',
    })
  },
   toTeacherContent:function(event){
     console.log(event)
     var id=event.currentTarget.dataset.id
     wx.navigateTo({
       url: '../teacher/teacher?id='+id,
     })
   },
   toCourseContent:function(event){
     var id=event.currentTarget.dataset.id
     wx.navigateTo({
       url: '/lesson/content/content?id='+id,
     })
   },
   toVideoContent:function(event){
     var that=this
     var id=event.currentTarget.dataset.id
     var isBought=event.currentTarget.dataset.buy
     if(!isBought){
       var p=event.currentTarget.dataset.price
       wx.showModal({
         title: '',
         content: '您当前还未购买视频，是否愿意花'+p+'积分购买视频',
         success:function(res){
           if(res.confirm){
            wx.request({
              url: app.globalData.baseUrl +'microvideo/video/'+id,
              method:'POST',
              header:{
                'cookie':'JSESSIONID='+wx.getStorageSync('sessionID'),
                'content-type': 'application/x-www-form-urlencoded'
              },
              success:function(res){
                console.log(res)
                if(res.data.code==0){
                  wx.showToast({
                    title: '购买成功',
                    duration:4000
                  })
                  that.onShow()
                  wx.navigateTo({
                    url: '/eduVedio/playVideo/playVideo?id=' + id,
                  })
                }
              }
            }) 
           }
         }
       })
     }else{
       wx.navigateTo({
         url: '/eduVedio/playVideo/playVideo?id=' + id,
       })
     }
   
   },
   
  /**
   * 生命周期函数--监听页面加载
   */
  gosearch:function(event){
     wx.navigateTo({
       url: '../search/search',
     })
  },
  toPintuan:function(event){
     wx.navigateTo({
       url: '../pintuan/pintuanTable/pintuanTable',
     })
  },
  onLoad: function (options) {
   var that=this
    wx.getStorage({
      key: 'know',
      success: function (res) {
        that.setData({
          read:false
        })
        app.isLogin()
        wx.getLocation({
          success: function (res) {
            console.log(res)

          },
        })
      },
      fail: function (res) {
        that.setData({
          read:true
        })
        wx.hideTabBar({
          
        })
        wx.getSystemInfo({
          success: function (res) {
            console.log(res)
            that.setData({
              windowHeight:res.screenHeight
            })
          },
        })
      }
    })
 
    // wx.showModal({
    //   title: '123',
    //   content: '请先登录',
    // })
    
  },
  know:function(res){
    wx.setStorageSync('know', 'true')
    wx.showTabBar({

    })
    this.setData({
      read:false
    })
    app.isLogin()
    wx.getLocation({
      success: function (res) {
        console.log(res)

      },
    })
  },
  callBackFun:function(res){
   console.log('轮播图')
   console.log(res)
     for(var index=0;index<5;index++){
       var src='lunboArray['+index+'].src'
       var id='lunboArray['+index+'].id'
       var url='lunboArray['+index+'].url'
       this.setData({
         [src]:res.data.data.dataList.data[index].file,
         [id]:res.data.data.dataList.data[index].id,
         [url]:res.data.data.dataList.data[index].url
       })
     }
  
  },
  //跳转到机构详情
  toCompanyContent:function(event){
    var id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '../xiangqing/xiangqing?id=' + id,
    })
  },
  //跳转到头条详情
  toNewsContent:function(event){
    var id=event.currentTarget.dataset.id
    wx.navigateTo({
      url: '/eduNews/newsContent/newsContent?id='+id,
    })
  },
  //选择当前位置
  toaddress:function(event)
  {
    var that=this
    wx.chooseLocation({
      success: function(res) {
        console.log(res)
        var address=res.name
        if(address.length>2){
          address=address.substring(0,2)+'..'
          that.setData({
            position:address
          })
        }else{
          that.setData({
            position:address
          })
        }     
      },
    })
  },
  toOnline:function(event){
    wx.navigateTo({
      url: '../../online/onlinePage/onlinePage',
    })
  },
  toLesson:function(event)
  {
    wx.navigateTo({
      url: '../../lesson/table/table',
    })
  },
  toVideo:function(event){
    wx.navigateTo({
      url: '../../eduVedio/videoPage/videoPage',
    })
  },
  toEducation:function(event)
  {
    wx.navigateTo({
      url: '../../eduNews/educationTable/educationTable',
    })
  },
  //跳转到教育资料
  toEdudata:function(event)
  {
    wx.navigateTo({
      url: '../../eduData/eduTable/eduTab'
    })
  },
  //跳转到在线读书
  toRead:function(event)
  {
    wx.navigateTo({
      url: '../../onlineRead/onlineRead/onlineRead',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
   
    console.log(wx.getStorageSync('sessionID'))
    var that = this
    app.qingqiu('chart/chart', { pageSize: 5, pageNo: 1 }, '', this.callBackFun)
    wx.request({
      url: app.globalData.baseUrl + 'wechat/index',
      method: 'GET',
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)
        try{
          for (var index = 0; index < 4; index++) {
            var nums = 'informationArray[' + index + '].nums'
            var title = 'informationArray[' + index + '].title'
            var id = 'informationArray[' + index + '].id'

            that.setData({
        
              [title]: res.data.data.news[index].title,
              [id]: res.data.data.news[index].id
            })
          }
        }catch(err){
          // wx.showToast({
          //   title: '获取教育头条失败',
          //   icon:'none'
          // })
          console.log(err)
        }
        try{
          for (var i = 0; i < 3; i++) {
            var c_id = 'companysArray[' + i + '].id'
            var c_src = 'companysArray[' + i + '].src'
            var c_name = 'companysArray[' + i + '].name'
            var n = res.data.data.mechanisms[i].name
            if(n.length>8){
              n=n.substring(0,7)+'..'
            }
            that.setData({
              [c_id]: res.data.data.mechanisms[i].id,
              [c_src]: res.data.data.mechanisms[i].img,
              [c_name]: n
            })
          }
        }catch(err){
          // wx.showToast({
          //   title: '获取机构数据失败',
          //   icon:'none'
          // })
          console.log(err)
        }
        try{
          for (var i = 0; i < 3; i++) {
            var name = 'teachersArray[' + i + '].name'
            var sex = 'teachersArray[' + i + '].sex'
            var sexBool
            if (res.data.data.teachers[i].entityData.user.gender == '0') {
              sexBool = true
            } else {
              sexBool = false
            }
            var distance = res.data.data.teachers[i].distance
            var danwei

            if (distance > 1000) {
              danwei = 'km'
              distance = parseInt(distance / 1000)

            } else {
              danwei = 'm'
              distance = parseInt(distance)
            }
            var position = 'teachersArray[' + i + '].position'
            var avatar = 'teachersArray[' + i + '].avatar'
            var id = 'teachersArray[' + i + '].id'
            var motto = 'teachersArray[' + i + '].motto'
            var subject = 'teachersArray[' + i + '].subject'
            var dan = 'teachersArray[' + i + '].dan'
            var distan = 'teachersArray[' + i + '].distan'
            that.setData({
              [name]: res.data.data.teachers[i].entityData.user.nickName,
              [sex]: sexBool,
              [position]: res.data.data.teachers[i].entityData.user.position,
              [avatar]: res.data.data.teachers[i].entityData.user.avatarUrl,
              [id]: res.data.data.teachers[i].entityData.id,
              [motto]: res.data.data.teachers[i].entityData.motto,
              [subject]: res.data.data.teachers[i].entityData.teacherType.name,
              [dan]: danwei,
              [distan]: distance
            })
          }
        }catch(err){
          // wx.showToast({
          //   title: '获取推荐老师数据失败',
          //   icon:'none'
          // })
          console.log(err)
        }        
        try{
          for (var i = 0; i < 3; i++) {
            var id = 'studentArray[' + i + '].id'
            var name = 'studentArray[' + i + '].name'
            var price = 'studentArray[' + i + '].price'
            var avatar = 'studentArray[' + i + '].avatar'
            var position = 'studentArray[' + i + '].position'
            var qingkuang = 'studentArray[' + i + '].qingkuang'
            var des = res.data.data.demands[i].description

            that.setData({
              [id]: res.data.data.demands[i].id,
              [name]: res.data.data.demands[i].title,
              [price]: res.data.data.demands[i].price,
              [avatar]: res.data.data.demands[i].userAvatarUrl,
              [position]: res.data.data.demands[i].position,
              [qingkuang]: res.data.data.demands[i].description.substring(0,10)
            })
          }
        }catch(err){
          // wx.showToast({
          //   title: '获取推荐学生数据失败',
          //   icon: 'none'
          // })
          console.log(err)
        }
       
      
        
        try{
          var courseData = res.data.data.courseCenterCourses
          console.log(courseData)
          for (var i = 0; i < 3; i++) {
            var id = 'lessonArray[' + i + '].id'
            var img = 'lessonArray[' + i + '].img'
            var title = 'lessonArray[' + i + '].title'
            //  var grade = 'lessonArray[' + i + '].grade'
            //  var subject = 'lessonArray[' + i + '].subject'
            //  var type = 'lessonArray[' + i + '].type'
            // console.log(courseData[i].id)
            var t = courseData[i].title
            if (t.length > 8) {
              t = t.substring(0, 6) + '...'
            }
            that.setData({
              [id]: courseData[i].id,
              [img]: courseData[i].img,
              [title]: t
            })
          }
        }catch(err){
          // wx.showToast({
          //   title: '获取推荐课程数据失败',
          //   icon: 'none'
          // })
          console.log(err)
        }
        try{
          for (var i = 0; i < 3; i++) {
            var coverImg = 'videoArray[' + i + '].coverImg'
            var title = 'videoArray[' + i + '].title'
            var id = 'videoArray[' + i + '].id'
            var read = 'videoArray[' + i + '].read'
            var buy='videoArray['+i+'].buy'
            var price='videoArray['+i+']price'
            var t = res.data.data.microVideos[i].title

            if(t.length>5){
              t=t.substring(0,4)+'...'
            }
            that.setData({
              [title]: t,
              [id]: res.data.data.microVideos[i].id,
              [read]: res.data.data.microVideos[i].read,
              [coverImg]:res.data.data.microVideos[i].coverImg,
              [buy]:res.data.data.microVideos[i].isBought,
              [price]:res.data.data.microVideos[i].price
            })
          } 
        }catch(err){
          // wx.showToast({
          //   title: '获取推荐课程数据失败',
          //   icon: 'none'
          // })
          console.log(err)
        }
       


      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.showToast({
      icon: 'none',
      title: '刷新成功'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})